import wx from '../../util/weixin'
import { placeOrder, getOrderList, getOrderDetails, getOrderTicketList, backOrder} from '../../service/api'

const state = {

    orderlist : [],

    orderinfo : {},

    orderticketlist : [],
}

const getters = {

    orderlist : state => state.orderlist,
    orderinfo : state => state.orderinfo,
    orderticketlist : state => state.orderticketlist,
}

const actions = {

    //下单连支付
    async createOrder ({ commit, state}, info) {
        let res = await placeOrder(
            info.para.sale_code,
            info.para.attr_id,
            info.para.num,
            info.para.mobile,
            info.para.total_price,
            info.para.name,
            info.para.tour_date,
            info.para.cardno,
            info.para.openid,
            info.para.remark,
        );
        if(res.errcode == 0){
            var p = {
                'ordercode' : res.data,
                'company_code' : info.company_code,
                'pay_company_code' : info.para.pay_company_code,
            }
            wx.wexinPay(p, (ordercode) => {
                info.callback(ordercode);
            });
        }else{
            info.error(res.errmsg);
        }
    },

    //订单列表
    async orderList ({ commit, state}, info) {
        let res = await getOrderList(info.para.pageNo, info.para.pageSize, info.para.pay_state);
        if(res.errcode == 0){
            commit('setOrderList', res.data);
            info.callback(res.data);
        }
    },


    //订单详情
    async orderInfo ({ commit, state}, info) {
        let res = await getOrderDetails(info.code);
        if(res.errcode == 0){
            commit('setOrderInfo', res.data);
            info.callback(res.data);
        }
    },

    //订单中票信息
    async orderTicketList ({ commit, state}, info) {
        let res = await getOrderTicketList(info.code);
        if(res.errcode == 0){
            commit('setOrderTicketList', res.data);
            info.callback(res.data);
        }
    },

    //退票
    async orderBack ({ commit, state}, info) {
        let res = await backOrder(info.code, info.count);
        if(res.errcode == 0){
            info.callback(res.data);
        }else{
            info.error(res.errmsg);
        }
    }

}

const mutations = {
    setOrderList (state, obj) {
        state.orderlist = obj;
    },
    setOrderInfo (state, obj) {
        state.orderinfo = obj;
    },
    setOrderTicketList (state, obj) {
        state.orderticketlist = obj;
    },
}

export default {
  state,
  getters,
  actions,
  mutations
}
